package com.dognessnetwork.customer.controller.json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.dognessnetwork.customer.domain.Person;
import com.dognessnetwork.customer.util.StringToNumber;

//import com.dognessnetwork.customer.service.api.RedisService;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@RestController
@RequestMapping(value = "/js/petUser", name = "PetUserLoginJson")
public class PetUserLoginJson {
    private final   Logger  log =   LoggerFactory.getLogger(this.getClass());

    /*@Autowired
    RedisService redisService;*/

    @Autowired
    HttpSession session;
    
    @Autowired
    Person  person;
    
    @Autowired
    Environment	evn;
    
    
    @PostMapping("/login_post")
    public JSONObject login_post(String username,String password,String	lang,Model	model) {
        if(lang==null){
        	lang="zh_CN";
        }
        JSONObject res = new JSONObject();
        JSONObject header = new JSONObject();
        JSONObject data = person.CheckLogin(username,password,evn);
        log.info("\n{},{},{}",username,person.getMaps(),data);
        header.put("status", 2000);
        header.put("message", lang);
        res.put("header", header);
        res.put("data", data);
        res.put("p", password);
        model.addAttribute("p", password);
        return res;
    }
    /**
     * /js/petUser/get_login_petUser 用给定的kEY从redis中获取value
     * 
     * @return
     */
    @GetMapping("/get_login_petUser")
    @ResponseBody
    public JSONObject get_login_petUser() {

        log.info("获取用户信息");
        JSONObject customer = (JSONObject) session.getAttribute("user");
        
        // customer.put("res", value);
        log.info("[]",customer);
        return customer;
    }

    /**
     * /js/petUser/requestCustomerService
     * 
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("/requestCustomerService")
    public JSONObject message(String username, String token, HttpServletRequest request, String platForm, String area) {
        log.info(username);
        //redisService.setStr(username, token);
        HttpSession	session	=	request.getSession();
        session.setAttribute(username, token);
        if (username == null)
            username = "hello";

        String petUser = session.getAttribute(username).toString();
        JSONObject js = JSONUtil.parseObj(petUser);
        js.getStr("");
        js.getStr("token");
        js.getStr("platForm");
        js.getStr("area");
        js.getStr("");
        js.getStr("");
        JSONObject mine = new JSONObject();
        mine.put("username", username);// 我的昵称
        mine.put("id", StringToNumber.letterToNumber(username));// 我的ID
        mine.put("status", "online");// 在线状态 online：在线、hide：隐身
        mine.put("sign", "PFU");// 我的签名
        mine.put("avatar", "/userchat/img/tu2.png");// 我的头像
        mine.put("platForm", platForm);
        // redisService.setStr(key, mine+"");
        log.info("登录");
        log.info("[]",mine);
        return /* "customer_service" */mine;
    }

    /**
     * /js/petUser/get_x_auth_token
     * 
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_x_auth_token")
    public JSONObject get_x_auth_token(String username, HttpServletRequest request) {
        log.info(request.getSession().getAttribute(username).toString());
        // Console.log(request.getSession().getAttribute(username).toString());;
        JSONObject res = new JSONObject();
        res.put("x-auth-token", request.getSession().getAttribute(username).toString());// 我的昵称
        return res;
    }

    /**
     * 获取客服信息
     * 
     * @return
     */
    @GetMapping("/get_customer")
    @ResponseBody
    public String get_customer(String   key,String	lang) {
       // String key = (String) session.getAttribute("user");
        log.info("\nget_customer()获取客服信息{}",key);
        String value = (String) session.getAttribute(key);
        log.info(value);

        return value;
    }
}
